package com.tzbank.wisdom.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.service.CommonService;
import com.tzbank.wisdom.utils.BusinessConstant;
import com.tzbank.wisdom.utils.CodeMsgTypeEnum;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.TransCodeEnum;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

@Service
@Slf4j
public class CommonServiceImpl implements CommonService {
	
	/**
	 * 发送短信动态码
	 * @param paramsMap
	 */
	public Map<String, Object> sendCodeMsg(Map<String, String> paramsMap) {
		String businessType = paramsMap.get("businessType");			//业务类型
		//贷款
		if(CodeMsgTypeEnum.LOAN.getValue().equals(businessType)) {
			paramsMap.put("smsInfo", "您正在进行贷款申请交易");
		}
		//注册
		if(CodeMsgTypeEnum.REGISTER.getValue().equals(businessType)) {
			paramsMap.put("smsInfo", "您正在进行注册身份校验");
			String mobileNo = paramsMap.get("mobileNo");		//手机号码
			if(StringUtils.isNotBlank(mobileNo) ) {				//判空
				paramsMap.put("mobile", mobileNo);				//赋值
			}
		}
		
		//智多薪签约
		if(CodeMsgTypeEnum.SAVE.getValue().equals(businessType)) {
			paramsMap.put("smsInfo", "您正在进行注册身份校验");
			String mobileNo = paramsMap.get("mobileNo");		//手机号码
			if(StringUtils.isNotBlank(mobileNo) ) {				//判空
				paramsMap.put("mobile", mobileNo);				//赋值
			}
		}
		//找回密码
		if(CodeMsgTypeEnum.FINDPASSWORD.getValue().equals(businessType)) {
			paramsMap.put("smsInfo", "您正在进行身份证校验交易");
		}
		//转账
		if(CodeMsgTypeEnum.TRANSFER.getValue().equals(businessType)) {
			//拼接短信内容
			StringBuffer smsInfo = new StringBuffer("您正在进行转账交易，收款人姓名: ");
			smsInfo.append(paramsMap.get("drAcctName")).append(",金额").append(paramsMap.get("amt")).append("元");	
			paramsMap.put("smsInfo", smsInfo.toString());
		}
		return sendMsgToMB(paramsMap);
	}

	/**
	 * 发送短信动态码
	 * @param paramsMap
	 */
	@Override
	public Map<String, Object> sendMsgToMB(Map<String, String> paramsMap) {
		log.info("发送短信动态码请求参数：{}", paramsMap);
		
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = new HashMap<String, Object>();

		String clientSeqNo = paramsMap.get("clientSeqNo");
		String mobile = paramsMap.get("mobile");
		String smsInfo = paramsMap.get("smsInfo");

		String clientNo = "";
		String selfUserId = "";
		String userSeqNo = "";

		if (!StringUtils.isEmpty(clientSeqNo)) {
			Map<?, ?> userMap = RedisUtils.getUserInfo(clientSeqNo);
			if (userMap != null) {
				clientNo = userMap.get("clientNo").toString();
				userSeqNo = userMap.get("userSeqNo").toString();
				selfUserId = userMap.get("selfUserId").toString();
			}
		}

		reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.TRANS_SENDMSG.getServiceCode(), TransCodeEnum.TRANS_SENDMSG.getServiceSence(), "0110");
		
		Map<String, Object> body = new HashMap<String, Object>();
		body.putAll(paramsMap);
		
		inputMap = RequestMap.getInputMap(body);
		
		inputMap.put("clientNo", clientNo);
		inputMap.put("eClientNo", clientSeqNo);
		inputMap.put("eUserNo", userSeqNo);
		inputMap.put("selfUserId", selfUserId);

		// 调用核心计价因子入参
		inputMap.put("sysHead", reqSysHead);
		inputMap.put("mobile", mobile); // 手机号
		inputMap.put("smsInfo", smsInfo); // 短信内容
		inputMap.put("smsSendFlag", "H");
		
		log.info("发送短信动态码请求开始...");
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
		log.info("发送短信动态码请求结束 响应结果: {}", result);
		if (CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap("获取成功", ResultCode.RESULT_CODE_SUCCESS.code(), null);
		}
		return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),null);

	}

	/**
	 * 校验短信动态码
	 * @param paramsMap
	 */
	@Override
	public Map<String, Object> verifyCode(Map<String, String> paramsMap) {
		Map<String, Object> inputMap = new HashMap<String, Object>();
		
		Map<String, Object> map =  new HashMap<>();
		map.putAll(paramsMap);
		
		inputMap = RequestMap.getInputMap(map);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)paramsMap.get("clientSeqNo"));
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
	
		inputMap.putAll(userMap);
		inputMap.put("needVeriSms", "0");			//验证短信
		inputMap.put("mobile", paramsMap.get("mobile"));		//手机号码
		inputMap.put("msgVerCode", paramsMap.get("msgVerCode"));	//短信验证码
		return this.sendEsb(inputMap);
	}
	
	
	/**
	 * 校验交易密码
	 * @param paramsMap
	 */
	@Override
	public Map<String, Object> verifyTransPassword(Map<String, String> paramsMap) {
		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> map =  JSONObject.fromObject(paramsMap);
		inputMap = RequestMap.getInputMap(map);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String)paramsMap.get("clientSeqNo"));
		if (userMap.get("clientNo")==null) {
			return userMap;
		}
	
		inputMap.putAll(userMap);
		inputMap.put("pwdCheckFlag", "0");        	//验证密码
		inputMap.put("password", paramsMap.get("password"));		//交易密码       
		inputMap.put("acctNo", paramsMap.get("acctNo"));  		//账号
		return this.sendEsb(inputMap);
	}
	
	/**
	 * 校验动态码公共部分
	 * @param inputMap
	 */
	public Map<String, Object> sendEsb(Map<String, Object> inputMap) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.TRANS_CHECKMSG.getServiceCode(), 
				TransCodeEnum.TRANS_CHECKMSG.getServiceSence(), "0110");
		inputMap.put("sysHead", reqSysHead);		//添加系统头
		log.info("校验请求开始...");
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);		//发送请求
		log.info("校验请求结束 响应结果: {}", result);
		String code = (String) result.get("code");		//返回状态
		//状态判断  
		if (!CodeUtil.CODE_200.getCode().equals(code)) {
			switch (code) {
			case BusinessConstant.VALIDATE_SMSCODE_OVERDUE:
				result.put("code", CodeUtil.CODE_10016.getCode());
				break;
			case BusinessConstant.VALIDATE_SMSCODE_FALSE:
				result.put("code", CodeUtil.CODE_10017.getCode());
				break;
			default:
				result.put("code", CodeUtil.CODE_99999.getCode());
				break;
			}
			return result;
		}
		return ResultUtil.resultMap("校验成功", ResultCode.RESULT_CODE_SUCCESS.code(), null);
	}
	
	
	

}